
import { GameplayComponent } from "../../GameplayComponent";
import ViewGameplay from "../../ViewGameplay";
import { Comp } from "../../m/CompDef";
import { GpEcsWorld } from "../../m/GpEcsWorld";
import { GpEventDef } from "../../m/GpEventDef";
import { GpCmdSystem, GpCmdType, GpCmdDataDef } from "../../m/systems/CmdSystem";
import { HeroSystem } from "../../m/systems/HeroSystem";
import { GpEntity } from "../GpEntity";
import GpHero from "../GpHero";
import GpControlPanel from "./GpControlPanel";
import { Vect3 } from "../../../common/MathUtils";



import { _decorator, Component, EventTouch, Node } from 'cc';
const { ccclass, property } = _decorator;

import { BaseComponent } from "../../../../../extensions/cocos-scaffold-3x/assets/ui/base/BaseComponent";
import { gg } from "../../../../Game";
import { convertToNodeSpaceAR, v2ToV3 } from "../../../../../extensions/cocos-scaffold-3x/assets/common/Utils";
import { EcsEntity } from "ecs-ts";
export enum ControlAction {
    none = 0,
    chooseRole = 1,
    att = 2,
    chooseMyHero = 3,
    setAtpZero = 4,
    addAtp = 5,
    kill = 6,
    autoAtt = 7,
    move = 8,
    jump = 9

}

@ccclass("GpActionPanel")
export default class GpActionPanel extends BaseComponent<GpControlPanel>{

    ecsWorld: GpEcsWorld
    onInit(): void {
        this.ecsWorld = this.prop.prop.ecsWorld
        gg.eventTarget.on(GpEventDef.onTouchEntity, this.onChooseTarget, this)
    }

    protected onDestroy(): void {
        gg.eventTarget.targetOff(this)
    }


    onChooseAction(e, actionId: string) {
        let currentChooseEntity: EcsEntity
        switch (Number(actionId)) {
            case ControlAction.chooseMyHero:
                const myHero = this.ecsWorld.system(HeroSystem).getMyHero()
                if (myHero) { this.prop.currentControlEntityId = myHero.id }
                break;
            case ControlAction.att:
                currentChooseEntity = this.ecsWorld.entities[this.prop.currentControlEntityId]
                if (!currentChooseEntity) {
                    alert("当前控制实体不存在")
                } else {
                    this.node.active = false
                    this.currentActionId = Number(actionId)
                }
                break;
            case ControlAction.setAtpZero:
                currentChooseEntity = this.ecsWorld.entities[this.prop.currentControlEntityId]
                if (!currentChooseEntity) {
                    alert("当前控制实体不存在")
                } else {
                    // currentChooseEntity.component(Comp.Battle.BattleInfo).atp = 0
                }
                break;
            case ControlAction.addAtp:
                currentChooseEntity = this.ecsWorld.entities[this.prop.currentControlEntityId]
                if (!currentChooseEntity) {
                    alert("当前控制实体不存在")
                } else {
                    // currentChooseEntity.component(Comp.Battle.BattleInfo).atp += 100
                    // console.log("atp", currentChooseEntity.component(Comp.Battle.BattleInfo).atp);

                }
                break;
            case ControlAction.autoAtt:
                currentChooseEntity = this.ecsWorld.entities[this.prop.currentControlEntityId]
                if (!currentChooseEntity) {
                    alert("当前控制实体不存在")
                } else {
                    currentChooseEntity.component(Comp.Battle.BattleInfo).enabelAutoAtt = !currentChooseEntity.component(Comp.Battle.BattleInfo).enabelAutoAtt
                    currentChooseEntity.component(Comp.Battle.BattleInfo).attTargetEntityId = 0
                }
                break;
            case ControlAction.move:
            case ControlAction.jump:
                currentChooseEntity = this.ecsWorld.entities[this.prop.currentControlEntityId]
                if (!currentChooseEntity) {
                    alert("当前控制实体不存在")
                    return
                }
            default:
                this.node.active = false
                this.currentActionId = Number(actionId)
                break;
        }
    }

    onChooseTarget(entity: GpEntity) {
        const ecsWorld = this.ecsWorld
        const cmdSystem = ecsWorld.system(GpCmdSystem)
        this.node.active = true
        if (!this.currentActionId) {
            return
        }
        if (this.currentActionId == ControlAction.chooseRole) {
            this.prop.currentControlEntityId = entity.ecsEntity.id
        } else if (this.currentActionId == ControlAction.att) {
            cmdSystem.makeCmd(
                GpCmdType.setAttTarget,
                new GpCmdDataDef.SetAttTarget(
                    this.prop.currentControlEntityId,
                    entity.ecsEntity.id,
                )
            )
        } else if (this.currentActionId == ControlAction.kill) {
            ecsWorld.removeEntity(entity.ecsEntity.id)
        }
        this.currentActionId = 0
    }

    onTouchActionTouchLayer(e: EventTouch) {
        let currentChooseEntity: EcsEntity = this.ecsWorld.entities[this.prop.currentControlEntityId]
        this.node.active = true

        if (!this.currentActionId) {
            return
        }
        // const pos = this.prop.prop.gpWorld.node.convertToNodeSpaceAR(e.touch.getLocation())
        const pos = convertToNodeSpaceAR(this.prop.prop.gpWorld.node, v2ToV3(e.touch.getLocation()))

        console.log(pos.x, pos.y);
        if (this.currentActionId == ControlAction.move) {
            currentChooseEntity.component(Comp.Move).hasForceMoveTargetPos = true
            Vect3.set(currentChooseEntity.component(Comp.Move).forceMoveTargetPos, pos.x, pos.y, 0)
        } else if (this.currentActionId == ControlAction.jump) {
            Vect3.set(currentChooseEntity.component(Comp.Transform).pos, pos.x, pos.y)
            currentChooseEntity.component(Comp.Move).hasForceMoveTargetPos = false
            currentChooseEntity.component(Comp.Battle.BattleInfo).attTargetEntityId = 0
            currentChooseEntity.component(Comp.Move).hasMoveTargetPos = false
        }
        this.currentActionId = 0

    }
    currentActionId: ControlAction = 0



}